package com.yd.mvc.mobile.evaluateController;

import com.yd.app.service.mobile.evaluateService.MEvaluateService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Evaluate;
import com.yd.domain.commom.PageModel;
import com.yd.domain.sys.User;
import com.yd.mvc.baseController.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 评价
 * Created by sunxianhui on 2018/3/12.
 */
@RestController
@RequestMapping(value = "/mapi/mevaluate")
public class MEvaluateController extends BaseController {
    @Autowired
    MEvaluateService evaluateService;

    /**
     * 添加
     * @param list
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addEvaluate(@RequestBody List<Evaluate> list, HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        for(Evaluate evaluate:list){
            evaluate.setPublishTime(new Date());
            evaluate.setPublishUser(u.getId());
            evaluateService.deleteEvaluate(evaluate.getOrderId(),evaluate.getGoodsId());
            int count= evaluateService.addEvaluate(evaluate);
//            if(count==1){
//                return evaluate;
//            }else {
//                throw new BusinessException(500, "添加评价信息失败");
//            }
        }
    }

    /**
     * 修改
     * @param evaluate
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateEvaluate(@RequestBody Evaluate evaluate) {
        evaluateService.updateEvaluate(evaluate);
    }

    /**
     * 删除
     * @param id
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public void deleteEvaluate(@PathVariable("id") String id) {
        evaluateService.deleteEvaluateByid(id);
    }

    /**
     * 根据orderId,goodsId查询评价
     * @param orderId
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/getEvaluate/{orderId}/{goodsId}", method = RequestMethod.GET)
    public Evaluate getEvaluate(@PathVariable("orderId") String orderId, @PathVariable("goodsId") String goodsId) throws BusinessException {
        Evaluate evaluate=evaluateService.getEvaluate(orderId,goodsId);
        return evaluate;
    }

    /**
     * 根据条件分页查询
     * @param goodsId
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/getList/{goodsId}/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageModel getEvaluateList(@PathVariable("goodsId") String goodsId, @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize) throws BusinessException {
        PageModel pageModel = null;
        try {
            pageModel = evaluateService.getEvaluateList(goodsId, pageIndex, pageSize);
        } catch (Exception e) {
            throw new BusinessException(500, "获取商品评价信息失败");
        }
        return pageModel;
    }
}
